package com.boot.system.controller;

import com.boot.framework.aop.Log;
import com.boot.framework.common.R;
import com.boot.system.dto.SysDictDTO;
import com.boot.system.service.ISysDictService;
import com.boot.system.wrapper.SysDictWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * @author yangfan
 * @date 2024/1/7
 * @Description
 */
@RestController
@AllArgsConstructor
@RequestMapping("/system/dict")
@Tag(name = "字典管理")
public class SysDictController {
    private final ISysDictService sysDictService;

    @GetMapping("page")
    @Operation(summary = "分页")
    public R page(@RequestParam Map<String, Object> params) {

        return R.ok(sysDictService.selectPage(params));
    }

    @GetMapping("list")
    @Operation(summary = "列表")
    public R list(@RequestParam Map<String, Object> params) {
        return R.ok(sysDictService.selectList(params));
    }


    @PostMapping
    @Log(title = "字典管理", content = "新增")
    @Operation(summary = "新增")
    public R insert(@RequestBody SysDictDTO sysDictDTO) {
        return R.ok(sysDictService.save(SysDictWrapper.build().dtoEntity(sysDictDTO)));
    }


    @PutMapping
    @Log(title = "字典管理", content = "修改")
    @Operation(summary = "修改")
    public R update(@RequestBody SysDictDTO sysDictDTO) {
        return R.ok(sysDictService.updateById(SysDictWrapper.build().dtoEntity(sysDictDTO)));
    }


    @GetMapping
    @Operation(summary = "查看")
    public R view(@RequestParam String id) {
        return R.ok(SysDictWrapper.build().entityDTO(sysDictService.getById(id)));
    }

    @DeleteMapping
    @Log(title = "字典管理", content = "删除")
    @Operation(summary = "删除")
    public R delete(String ids) {
        return R.ok(sysDictService.removeBatchByIds(Arrays.asList(StringUtils.split(ids, ","))));
    }

    @GetMapping("store")
    @Operation(summary = "缓存")
    public R store() {
        return R.ok(sysDictService.getStore());
    }
}
